package com.face.zf.controller;

import com.face.zf.annotation.RequestTimeMonitor;
import com.face.zf.domain.dto.Image;
import com.face.zf.domain.pojo.FacePackage;
import com.face.zf.service.FaceCompareService;
import com.face.zf.util.Base64Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.concurrent.*;

/**
 * @Author: (ﾉ ◠ ‿ ◠)ﾉ wangYu
 * @CreateTime: 2025-07-10
 * @ContactMe WECHAT-> wy10012311   QQ-> 1569350611
 * @Description: 包含了Pirs人脸比对算法和SmartJava人脸识别算法请求接口
 */

@RestController
@Slf4j
public class FaceApi {

    @Autowired
    FaceCompareService faceCompareService;

    @PostMapping("/compare")
    @RequestTimeMonitor
    public ResponseEntity<?> faceCompare(@RequestBody Image image) throws ExecutionException, InterruptedException, TimeoutException {
        if (image == null || image.getImage1Base64() == null || image.getImage2Base64() == null) {
            return ResponseEntity.badRequest().body("参数错误：图片数据不能为空");
        }
        ExecutorService executor = Executors.newSingleThreadExecutor();
        try {
            CompletableFuture<FacePackage> future = CompletableFuture.supplyAsync(() ->
                    faceCompareService.compare(image.getImage1Base64(), image.getImage2Base64()), executor);
            FacePackage result = future.get(10, TimeUnit.SECONDS);
            if (result.getCode() == 200) {
                return ResponseEntity.ok(result);
            } else {
                return ResponseEntity.status(result.getCode()).body(result);
            }
        }catch (Exception e) {
            return ResponseEntity.status(500).body("系统错误，请稍后重试");
        }
        finally {
            executor.shutdown();
        }
    }

//    @PostMapping("/query-general-data/service")
//    public String queryGeneralData(HttpServletRequest request) throws IOException {
//        return faceCompareService.pirsCompare(request);
//    }

    public static void main(String[] args) throws IOException {
        String s = Base64Utils.convertImageToBase64("D:\\face\\1.jpg");
        System.out.println(s);
//        System.out.println("========================");
//        String s2 = Base64Utils.convertImageToBase64("D:\\2020年直到今天\\1.png");
//        s2 = s2.replace("\n", "").replace("\r", "");
//        System.out.println(s2);
//        String s2 = Base64Utils.convertImageToBase64("D:\\face\\4.jpg");
//        System.out.println(s2);
    }
}
